//
//  BLPhotosView.m
//  新浪微博
//
//  Created by hbl on 16/2/27.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLPhotosView.h"
#import "BLPhotoView.h"
#import "BLPhoto.h"
#import "SDPhotoBrowser.h"
#import "UIImageView+WebCache.h"
#define PhotoWH 110
#define PhotoMargin 5
@interface BLPhotosView()<SDPhotoBrowserDelegate>

@end
@implementation BLPhotosView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        for (int i = 0; i<9; i++) {
            BLPhotoView *photoView = [[BLPhotoView alloc]init];
            photoView.tag = i;
            photoView.userInteractionEnabled = YES;
            [photoView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(photoTap:)]];
            [self addSubview:photoView];
            
        }
    }
    
    return self;
}

- (void)photoTap:(UITapGestureRecognizer *)tap
{
    SDPhotoBrowser *browser = [[SDPhotoBrowser alloc]init];
    browser.sourceImagesContainerView = tap.view.superview;
    browser.imageCount = self.photos.count;
    browser.currentImageIndex = tap.view.tag;
    browser.delegate = self;
    [browser show];
   
}

- (void)setPhotos:(NSArray *)photos
{
    _photos = photos;
   
    for (int i = 0; i<self.subviews.count; i++) {
        BLPhotoView *imgView = self.subviews[i];
        
        if (i<photos.count) {
            imgView.hidden = NO;
            imgView.photo = photos[i];
            int maxColums = (photos.count == 4)?2:3;
            int row = i/maxColums;
            int colu = i%maxColums ;
            CGFloat photoX = colu * (PhotoMargin + PhotoWH);
            CGFloat photoY = row * (PhotoWH + PhotoMargin);
            CGFloat photoW = PhotoWH;
            CGFloat photoH = PhotoWH;
            imgView.frame = CGRectMake(photoX, photoY, photoW, photoH);
            if (photos.count == 1) {
                imgView.contentMode = UIViewContentModeScaleAspectFit;
                imgView.clipsToBounds = NO;
            }else{
                imgView.contentMode = UIViewContentModeScaleAspectFill;
                imgView.clipsToBounds = YES;
            }
        }else{
            imgView.hidden = YES;
        }
    }
}

//- (void)layoutSubviews
//{
//    [super layoutSubviews];
//    NSLog(@"%@",self);
//}

+ (CGSize)photosViewSizeWithPhotosCount:(int)count
{
    // 一行最多有3列
    int maxColumns = (count == 4) ? 2 : 3;
    
    //  总行数
    int rows = (count + maxColumns - 1) / maxColumns;
    // 高度
    CGFloat photosH = rows * PhotoWH + (rows - 1) * PhotoMargin;
    
    // 总列数
    int cols = (count >= maxColumns) ? maxColumns : count;
    // 宽度
    CGFloat photosW = cols * PhotoWH + (cols - 1) * PhotoMargin;
    
    return CGSizeMake(photosW, photosH);
    /**
     一共60条数据 == count
     一页10条 == size
     总页数 == pages
     pages = (count + size - 1)/size;
     */
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    BLPhoto *photo = self.photos[index];
    NSString *urlString = [photo.thumbnail_pic stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
    return [NSURL URLWithString:urlString];
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    BLPhoto *photo = self.photos[index];
    UIImageView *imgView = [[UIImageView alloc]init];
    [imgView sd_setImageWithURL:[NSURL URLWithString:photo.thumbnail_pic]];
    return imgView.image;
}
@end
